#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"QMenuBar"
#include"QFileDialog"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
   QMenuBar* menubar=this->menuBar();
   QStatusBar* statusbar=this->statusBar();
   this->setStatusBar(statusbar);
   QMenu* menu=new QMenu("文件");
   QAction* action1=new QAction("打开");
   QAction*action2=new QAction("保存");
   menu->addAction(action1);
   menu->addAction(action2);
   this->setMenuBar(menubar);
    menubar->addMenu(menu);
     // 关键修改：使用triggered信号而不是toggled
    connect(action1,&QAction::triggered,this,&MainWindow::openfile);
    connect(action2,&QAction::triggered,this,&MainWindow::savefile);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::openfile()
{
    QString path=QFileDialog::getOpenFileName(this);
   QFile file(path);

   file.open(QIODevice::ReadOnly);
   if(!file.isOpen())
   {
       ui->statusbar->showMessage("打开失败");
   }
    ui->statusbar->showMessage(path);
    QString text=file.readAll();

   file.close();
   ui->textEdit->setText(text);
}

void MainWindow::savefile()
{
    QString path=QFileDialog::getSaveFileName(this);
    QFile file(path);
    file.open(QIODevice::WriteOnly);
    if(!file.isOpen())
    {
        ui->statusbar->showMessage("打开失败");
    }
     ui->statusbar->showMessage(path);
     QString text=ui->textEdit->toPlainText();

     file.write(text.toUtf8());
     file.close();


}

